<template>
  <div class="login">
    <div class="login_l">
      <h3>平台登录</h3>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
        class="demo-ruleForm"
        
      >
        <el-form-item label="账号" prop="name">
          <el-input v-model="ruleForm.name" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" style="width: 200px"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="验证码" prop="">
          <el-input  style="width: 200px"></el-input>
        </el-form-item> -->
        <!-- 登录 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        name: "",
        password: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入账号", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 表单验证成功
            var str = `username=${this.ruleForm.name}&password=${this.ruleForm.password}`;
            this.axios.post("login",str).then(res=>{
              console.log(res);
              if (res.data.meta.status == 200) {
                this.$message({
                  showClose: true,
                  message:res.data.meta.msg,
                  type: 'success'
                });
                // 存储token
                sessionStorage.setItem("token",res.data.data.token);
                sessionStorage.setItem("uname",res.data.data.username);
                // 跳转
                this.$router.push("/")
              }else{
                this.$message(res.data.meta.msg)
              }
            })
          } else {
            
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-image: url("../assets/map.jpg");
  background-repeat: no-repeat;
  background-size: contain;
  margin-top: 80px;
  text-align: center;
  position: fixed;
}
.login_l {
  width: 300px;
  height: 350px;
  background: white;
  margin-left: 60%;
  margin-top: 50px;
}
</style>
